package org.niit.bean

import java.sql.Timestamp

/**
 * 订单实体类
 *
 * @param orderId 订单ID
 * @param userId 用户ID
 * @param age 年龄
 * @param gender 性别
 * @param merchantName 商铺名称
 * @param dishName 菜品名称
 * @param category 菜品类别
 * @param price 价格
 * @param city 城市
 * @param timePeriod 时间段
 * @param platform 平台
 * @param rating 评分
 * @param orderTime 下单时间
 */
case class Order(
  orderId: String,
  userId: String,
  age: Int,
  gender: String,
  merchantName: String,
  dishName: String,
  category: String,
  price: Double,
  city: String,
  timePeriod: String,
  platform: String,
  rating: Double,
  orderTime: Timestamp
)

/**
 * 订单伴生对象
 */
object Order {
  /**
   * 从CSV格式的字符串转换为Order对象
   *
   * @param line CSV格式的订单数据行
   * @return Order对象
   */
  def parseFromCsv(line: String): Order = {
    try {
      val fields = line.split(",", -1)
      val timestamp = if (fields(12).trim.nonEmpty) {
        // 将日期时间字符串转换为Timestamp
        java.sql.Timestamp.valueOf(fields(12))
      } else {
        new java.sql.Timestamp(System.currentTimeMillis())
      }
      
      Order(
        orderId = fields(0),
        userId = fields(1),
        age = fields(2).toInt,
        gender = fields(3),
        merchantName = fields(4),
        dishName = fields(5),
        category = fields(6),
        price = fields(7).toDouble,
        city = fields(8),
        timePeriod = fields(9),
        platform = fields(10),
        rating = fields(11).toDouble,
        orderTime = timestamp
      )
    } catch {
      case e: Exception =>
        println(s"解析订单数据失败: $line")
        e.printStackTrace()
        // 返回一个默认值或抛出异常
        null
    }
  }
} 